
#include <fstream>
#include <utility>
#include <vector>

typedef std::pair<double,double> pairdd;
typedef std::vector<pairdd> pairvecdd;

/**/
pairvecdd read_pairs_from_file( const char* filename )
{
  pairvecdd res;
  std::ifstream fin( filename, std::ios_base::in );
  double x(0.0), y(0.0);
  while( !fin.eof() ) {
    fin >> x >> y;
    res.push_back( std::make_pair(x,y) );
  }
  fin.close();
  return res;
}

double trapezoid_area( const pairdd& pa, const pairdd& pb )
{
  auto b = pb.first -   pa.first;
  auto h = (pa.second + pb.second) / 2.0;
  return b * h;
}

/**/
double area_under_plot( const pairvecdd& pairs )
{
  double res(0.0);
  pairvecdd::const_iterator ci = pairs.begin();
  pairvecdd::const_iterator ce = pairs.end() - 1;
  while( ci != ce ) {
    res += trapezoid_area( *ci, *(ci+1) );
    ++ci;
  }
  return res;
}


/*
 * 
 */
int main( int argc, char** argv )
{
  pairvecdd pv = read_pairs_from_file("D:/Projects/code-storage-my/lisp/test1.txt");
  double rs = area_under_plot( pv );
  return 0;
}

